package com.qrxl.user.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qrxl.common.JsonResult;
import com.qrxl.common.controller.BaseController;
import com.qrxl.common.vo.BaseQuery;
import com.qrxl.common.vo.req.IdReq;
import com.qrxl.enums.EnumUserType;
import com.qrxl.user.entity.Menu;
import com.qrxl.user.service.IMenuService;
import com.qrxl.user.vo.UserLoginInfo;
import com.qrxl.user.vo.req.MenuAddReq;
import com.qrxl.user.vo.req.MenuListReq;
import com.qrxl.user.vo.req.MenuUpdateReq;
import com.qrxl.user.vo.res.MenuTreeRes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单 前端控制器
 * </p>
 *
 * @author friday
 * @since 2020-12-05
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/user/menu")
public class MenuController extends BaseController {
    @Autowired
    IMenuService menuService;

    @ResponseBody
    @PostMapping("/v1/add")
    @ApiOperation(value = "菜单新增")
    public JsonResult register(@RequestBody @Validated MenuAddReq req) {
        menuService.add(req);
        return JsonResult.genJsonResultByOk();
    }

    @ResponseBody
    @GetMapping("/v1/queryList")
    @ApiOperation(value = "菜单列表")
    public JsonResult<IPage<Menu>> list(@Validated BaseQuery baseQuery, MenuListReq req) {
        return JsonResult.genJsonResultByOk(menuService.selectPage(baseQuery, req));
    }

    @ResponseBody
    @PostMapping("/v1/update")
    @ApiOperation(value = "菜单编辑")
    public JsonResult update(@Validated @RequestBody MenuUpdateReq req) {
        menuService.updateVo(req);
        return JsonResult.genJsonResultByOk();
    }

    @ResponseBody
    @PostMapping("/v1/delete")
    @ApiOperation(value = "菜单删除")
    public JsonResult update(@Validated @RequestBody IdReq req) {
        menuService.delete(req.getId());
        return JsonResult.genJsonResultByOk();
    }

    @ResponseBody
    @GetMapping("/v1/queryMenuTree")
    @ApiOperation(value = "菜单树形结构")
    public JsonResult<List<MenuTreeRes>> queryMenuTree() {
        UserLoginInfo ul = getUserLoginInfo();
        List<MenuTreeRes> menuTree;
        if (EnumUserType.SUPPER_ADMIN.getCode().equals(ul.getType())) {
            //超级平台拥有所有菜单
            menuTree = menuService.queryAllMenu();
        } else {
            //获取分配的菜单
            menuTree = menuService.qeryUserMenus(ul);
        }
        return JsonResult.genJsonResultByOk(menuTree);
    }
}

